package com.sphd.beautysalon.activitys.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.beautysalon.adapters.ManagerAdapter;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.presenter.DianzhangPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.DianzhangView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.info.ManagerInfo;

import org.litepal.crud.DataSupport;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DianzhangListActivity extends BaseActivity implements DianzhangView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    DianzhangPresenterImpl dianzhangPresenter;
    LoginInfo loginInfo;
    ManagerAdapter managerAdapter;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dianzhang_list);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constant.MANAGERINFO, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context,AddManagerActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private void initView() {
        intent=getIntent();
        setTitleBar(toolbar, "店长列表");
        dianzhangPresenter = new DianzhangPresenterImpl(this);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        initAdapter();
        addData();
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        managerAdapter = new ManagerAdapter(R.layout.item_manager, new ArrayList<>());
        myRecy.setAdapter(managerAdapter);
    }

    private void addData() {
        dianzhangPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.DianzhangList + "superid=" + loginInfo.getSuperid());
    }

    @Override
    public void recRes(List<ManagerInfo> managerInfos) {
        managerAdapter.setNewData(managerInfos);
        addFootView(managerAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            addData();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
